// pages/Census/customer-list.js
const Controllers = require('../../controllers/ControllerCensus').controller;
const shopDetails = require('../../controllers/Controller').controller.shopDetails;
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopId: '',
    tabs: [{
        value: 10000.00,
        text: '店铺租金收益',
        key: 'rent',
        type: 8
      },
      {
        value: 10000.00,
        text: '云商品收益',
        key: 'cloud',
        type: 7
      }
    ],
    currentIndex: 0,
    pageNumber: 1,
    list: [],
    totalRow: 0,
    isLoading: false,
    lastPage: true,



    type:'',
    startTime:'',
    endTime:'',
    userData:{
      title:'门店用户统计',
      list:[{
        text:'用户总数',
        num:0
      },{
        text:'会员数量',
        num:0
      }]
    },
    activeData:{
      title:'商圈活动统计',
      list:[{
        text:'活动总数',
        num:0
      },{
        text:'一卡通',
        num:0
      },{
        text:'店铺爆款',
        num:0
      },{
        text:'超值礼包',
        num:0
      },{
        text:'欢乐拼',
        num:0
      },{
        text:'抵扣券',
        num:0
      }]
    },
    tabList:['全部','一卡通','店铺爆款','超值礼包','欢乐拼','抵扣券']
  },
  changeTab(e) {
    let type = null
    const currentIndex = e.currentTarget.dataset.index
    switch(currentIndex){
      case 1:
        type = 0;
        break;
      case 2:
        type = 2;
        break;
      case 3:
        type = 1;
        break;
      case 4:
        type = 3;
        break;
      case 5:
        type = 4;
        break;
    }
    this.setData({
      type,
      currentIndex,
      pageNumber: 1,

    })
    this.shopBagPage();
  },
  // 获取店铺信息
  shopDataDetail() {
    var that = this;
    const shopId = that.data.shopId;
    Controllers.shopDataDetail({
      shopId
    }).then(res => {
      const shopData = res.data
      shopData.createTime = shopData.createTime.substr(0, 10).replace(/-/g, '.')
      shopData.endTime = shopData.endTime.substr(0, 10).replace(/-/g, '.')
        switch (shopData.state) {
          case 0:
            shopData.stateName = '已入驻';
            shopData.stateColor = 'green';
            break;
          case 1:
            shopData.stateName = '冻结';
            shopData.stateColor = 'red';
            break;
          case 2:
            shopData.stateName = '待审核';
            shopData.stateColor = 'orange';
            break;
          case 3:
            shopData.stateName = '审核失败';
            shopData.stateColor = 'red';
            break;
        }
      that.setData({
        shopData
      })
    })
  },
// 获取统计数据
  shopDetails() {
    var that = this;
    const shopId = that.data.shopId;
    shopDetails({
      shopId
    }).then(res => {
      const {totalAccountNum,vipAccountNum,groupCouponNum,groupHotNum,groupGiftNum,groupPinNum,groupLimitNum} = res.data
      const groupAllNum = groupCouponNum + groupHotNum + groupGiftNum + groupPinNum + groupLimitNum
      that.setData({
        'userData.list[0].num':totalAccountNum,
        'userData.list[1].num':vipAccountNum,
        'activeData.list[0].num':groupAllNum,
        'activeData.list[1].num':groupCouponNum,
        'activeData.list[2].num':groupHotNum,
        'activeData.list[3].num':groupGiftNum,
        'activeData.list[4].num':groupPinNum,
        'activeData.list[5].num':groupLimitNum,
      })
    })
  },

  startTimeChange(e) {
    this.setData({
      startTime: e.detail.value
    })
  },

  endTimeChange(e) {
    this.setData({
      endTime: e.detail.value
    })
  },

  // 活动列表
  shopBagPage(isFirst = true){
    if(isFirst){
      this.setData({
        list: [],
        pageNumber:1
      })
    }

    if (this.data.isLoading) {
      return;
    }
    this.setData({
      isLoading: true
    })
    wx.showLoading();

    let type = this.data.type
    let shopId = this.data.shopId
    let endTime = this.data.endTime
    let startTime = this.data.startTime
    let pageNumber = this.data.pageNumber
    let value = {
      shopId,
      pageNumber,
    }
    if(type)value.type = type
    if(endTime)value.endTime = endTime
    if(startTime)value.startTime = startTime
    Controllers.shopBagPage(value).then(res=>{
      let {list,lastPage} = res.data
      list = this.data.list.concat(list);
      
      this.setData({
        list,
        lastPage,
        pageNumber:pageNumber+1,
        isLoading: false
      });
      wx.hideLoading();
    })
  },



  onLoad: function (options) {
    var that = this;
    that.setData({
      shopId: options.id
    })
  },

  onShow: function () {
    this.shopDetails();
    this.shopDataDetail();
    this.shopBagPage();
  },

  onReachBottom: function () {
    if (this.data.isLoading || this.data.lastPage)return;
    console.log(11111111111);
    this.shopBagPage(false);
  },
  // shopDataDetailPage() {
  //   var that = this;
  //   const pageNumber = this.data.pageNumber
  //   if (that.data.isLoading) {
  //     return;
  //   }
  //   that.data.isLoading = true;
  //   console.log('that.data.tabs[that.data.active==', that.data.tabs[that.data.active]);

  //   const type = that.data.tabs[that.data.active].type;
  //   wx.showLoading();
  //   var params = {
  //     pageNumber,
  //     shopId: that.data.shopId,
  //     type
  //   };
  //   Controllers.shopDataDetailPage(params).then(res => {
  //     const {
  //       list,
  //       totalRow,
  //       lastPage
  //     } = res.data;
  //     if (pageNumber === 1) {
  //       that.data.list = list;
  //     } else {
  //       that.data.list = that.data.list.concat(list);
  //     }
  //     that.setData({
  //       list: that.data.list,
  //       totalRow,
  //       lastPage,
  //       isLoading: false
  //     });
  //     wx.hideLoading();
  //   })
  // },


})